#ifdef __APPLE__
    #include <SDL/SDL.h>
    #include "SDL/SDL_opengl.h"
    #include "SDL/SDL_image.h"
#else
    #include <SDL/SDL.h>
    #include "GL/gl.h"
    #include "GL/glu.h"
    #include "SDL/SDL_image.h"
#endif
#include "BM_menu.h"
#include "BM_time.h"

#define FPS 60

extern void logMessage(const char *message, ...);

// load our textures and create our meshes
BMMenu::BMMenu()
{
    //loadObjects();

    selection = 1;
    menuEntries = 4;
}

void BMMenu::end()
{
    freeObjects();
}

void BMMenu::freeObjects()
{
    objectHandler[0].freeObject();
    objectHandler[1].freeObject();
    objectHandler[2].freeObject();
    objectHandler[3].freeObject();
}

void BMMenu::loadObjects()
{
    objectHandler[0].createObject(1,"Data/menu/1");
    objectHandler[1].createObject(1,"Data/menu/2");
    objectHandler[2].createObject(1,"Data/menu/3");
    objectHandler[3].createObject(1,"Data/menu/4");
}

/***************************************************************************/

int BMMenu::run_menu()
{
    SDL_Event event;

    int returnGame = 0;

	while(1){

        timer.start();

		SDL_PollEvent( &event );
		if( event.type == SDL_QUIT ){
			break;
        }

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        draw_menu();

        SDL_GL_SwapBuffers();

        keyboard.poll();

        if(keyboard.press(SDLK_DOWN))
        {
            if (selection < menuEntries)
                selection++;
        }
        else if(keyboard.press(SDLK_UP))
        {
            if (selection > 1)
                selection--;
        }
        else if(keyboard.press(SDLK_RETURN))
        {
            switch(selection)
            {
                case 1:
                    // start game
                    returnGame = 1;
                break;
                case 2:
                    // options
                    returnGame = 2;
                break;
                case 3:
                    // credits
                    returnGame = 3;
                break;
                case 4:
                    // exit
                    returnGame = 4;
                break;
            }
        }

        if(timer.getTicks() < 1000 / FPS)
        {
            SDL_Delay((1000/FPS)-timer.getTicks());
        }

        if (returnGame > 0)
            break;

	}

	return returnGame;
}

/***************************************************************************/

// draw our menu
void BMMenu::draw_menu()
{
    switch (selection)
    {
        case 1:
            objectHandler[0].drawObject(1, 0.0f, 3.0f, -15.0f);
            objectHandler[1].drawObject(0, 0.0f, 1.0f, -15.0f);
            objectHandler[2].drawObject(0, 0.0f, -1.5f, -15.0f);
            objectHandler[3].drawObject(0, 0.0f, -3.5f, -15.0f);
            objectHandler[0].angle -= 1.0f;
            objectHandler[1].angle -= 0.0f;
            objectHandler[2].angle -= 0.0f;
            objectHandler[3].angle -= 0.0f;
        break;
        case 2:
            objectHandler[0].drawObject(0, 0.0f, 3.0f, -15.0f);
            objectHandler[1].drawObject(1, 0.0f, 1.0f, -15.0f);
            objectHandler[2].drawObject(0, 0.0f, -1.5f, -15.0f);
            objectHandler[3].drawObject(0, 0.0f, -3.5f, -15.0f);

            objectHandler[0].angle -= 0.0f;
            objectHandler[1].angle -= 1.0f;
            objectHandler[2].angle -= 0.0f;
            objectHandler[3].angle -= 0.0f;
        break;
        case 3:
            objectHandler[0].drawObject(0, 0.0f, 3.0f, -15.0f);
            objectHandler[1].drawObject(0, 0.0f, 1.0f, -15.0f);
            objectHandler[2].drawObject(1, 0.0f, -1.5f, -15.0f);
            objectHandler[3].drawObject(0, 0.0f, -3.5f, -15.0f);

            objectHandler[0].angle -= 0.0f;
            objectHandler[1].angle -= 0.0f;
            objectHandler[2].angle -= 1.0f;
            objectHandler[3].angle -= 0.0f;
        break;
        case 4:
            objectHandler[0].drawObject(0, 0.0f, 3.0f, -15.0f);
            objectHandler[1].drawObject(0, 0.0f, 1.0f, -15.0f);
            objectHandler[2].drawObject(0, 0.0f, -1.5f, -15.0f);
            objectHandler[3].drawObject(1, 0.0f, -3.5f, -15.0f);

            objectHandler[0].angle -= 0.0f;
            objectHandler[1].angle -= 0.0f;
            objectHandler[2].angle -= 0.0f;
            objectHandler[3].angle -= 1.0f;
            //objects[3].xrot = 1.0f;
        break;
    }

    /*int err = glGetError();

    if (err != GL_NO_ERROR)
    {
        cout << "Error: " << err;
    }*/
}

